home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / toolbox.py < prev    next >
Text File  |  2009-10-09  |  8KB  |  266 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22. # Thanks to Henrique M. Holschuh <hmh@debian.org> for various security patches
  23. #
  24.  
  25. __version__ = '15.0'
  26. __mod__ = 'hp-toolbox'
  27. __title__ = 'HP Device Manager'
  28. __doc__ = """The HP Device Manager (aka "Toolbox") for HPLIP supported devices. Provides access to status, tools, and supplies levels."""
  29.  
  30. # Std Lib
  31. import sys
  32. import os
  33. import getopt
  34. import signal
  35.  
  36. # Local
  37. from base.g import *
  38. import base.utils as utils
  39. from base import status, tui, module
  40.  
  41.  
  42. w = None # write pipe
  43. app = None
  44. toolbox  = None
  45. session_bus = None
  46.  
  47.  
  48. def handle_session_signal(*args, **kwds):
  49.     if kwds['interface'] == 'com.hplip.Toolbox' and \
  50.         kwds['member'] == 'Event':
  51.  
  52.         event = device.Event(*args)
  53.         event.debug()
  54.  
  55.         if event.event_code > EVENT_MAX_EVENT:
  56.             event.event_code = status.MapPJLErrorCode(event.event_code)
  57.  
  58.         # regular user/device status event
  59.         log.debug("Received event notifier: %d" % event.event_code)
  60.  
  61.         if not event.send_via_pipe(w, 'toolbox ui'):
  62.             sys.exit(1)
  63.             # if this fails, then hp-toolbox must be killed.
  64.             # No need to continue running...
  65.  
  66.  
  67. mod = module.Module(__mod__, __title__, __version__, __doc__, None,
  68.                     (GUI_MODE,), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
  69. mod.lockInstance()
  70.  
  71. mod.setUsage(module.USAGE_FLAG_NONE,
  72.              extra_options=[("Disable dbus (Qt3 only):", "-x or --disable-dbus", "option", False)],
  73.              see_also_list = ['hp-align', 'hp-clean', 'hp-colorcal', 'hp-devicesettings',
  74.                               'hp-hp-faxsetup', 'hp-firmware', 'hp-info', 'hp-levels',
  75.                               'hp-linefeedcal', 'hp-makecopies', 'hp-plugin',
  76.                               'hp-pqdiag', 'hp-print', 'hp-printsettings', 'hp-scan',
  77.                               'hp-sendfax', 'hp-testpage', 'hp-timedate', 'hp-unload'])
  78.  
  79. opts, device_uri, printer_name, mode, ui_toolkit, loc = \
  80.     mod.parseStdOpts('x', ['disable-dbus'])
  81.  
  82. disable_dbus = False
  83.  
  84. for o, a in opts:
  85.     if o in ('-x', '--disable-dbus') and ui_toolkit == 'qt3':
  86.         disable_dbus = True
  87.  
  88. if ui_toolkit == 'qt3':
  89.     if not utils.canEnterGUIMode():
  90.         log.error("%s requires GUI support. Exiting." % __mod__)
  91.         sys.exit(1)
  92. elif ui_toolkit == 'qt4':
  93.     if not utils.canEnterGUIMode4():
  94.         log.error("%s requires GUI support. Exiting." % __mod__)
  95.         sys.exit(1)
  96.  
  97. child_pid, w, r = 0, 0, 0
  98.  
  99. if ui_toolkit == 'qt3':
  100.     try:
  101.         from dbus import SessionBus
  102.         import dbus.service
  103.         from dbus.mainloop.glib import DBusGMainLoop
  104.         from gobject import MainLoop
  105.     except ImportError:
  106.         log.error("Unable to load dbus - Automatic status updates in HPLIP Device Manager will be disabled.")
  107.         disable_dbus = True
  108.  
  109.     if not disable_dbus:
  110.         r, w = os.pipe()
  111.         parent_pid = os.getpid()
  112.         log.debug("Parent PID=%d" % parent_pid)
  113.         child_pid = os.fork()
  114.  
  115.     if disable_dbus or child_pid: # qt3/ui
  116.         # parent (UI)
  117.         log.set_module("hp-toolbox(UI)")
  118.  
  119.         if w:
  120.             os.close(w)
  121.  
  122.         try:
  123.             from qt import *
  124.             from ui.devmgr4 import DevMgr4
  125.         except ImportError:
  126.             log.error("Unable to load Qt3 support. Is it installed?")
  127.             sys.exit(1)
  128.  
  129.         # create the main application object
  130.         app = QApplication(sys.argv)
  131.  
  132.         if loc is None:
  133.             loc = user_conf.get('ui', 'loc', 'system')
  134.             if loc.lower() == 'system':
  135.                 loc = str(QTextCodec.locale())
  136.                 log.debug("Using system locale: %s" % loc)
  137.  
  138.         if loc.lower() != 'c':
  139.             e = 'utf8'
  140.             try:
  141.                 l, x = loc.split('.')
  142.                 loc = '.'.join([l, e])
  143.             except ValueError:
  144.                 l = loc
  145.                 loc = '.'.join([loc, e])
  146.  
  147.             log.debug("Trying to load .qm file for %s locale." % loc)
  148.             trans = QTranslator(None)
  149.  
  150.             qm_file = 'hplip_%s.qm' % l
  151.             log.debug("Name of .qm file: %s" % qm_file)
  152.             loaded = trans.load(qm_file, prop.localization_dir)
  153.  
  154.             if loaded:
  155.                 app.installTranslator(trans)
  156.             else:
  157.                 loc = 'c'
  158.  
  159.         if loc == 'c':
  160.             log.debug("Using default 'C' locale")
  161.         else:
  162.             log.debug("Using locale: %s" % loc)
  163.             QLocale.setDefault(QLocale(loc))
  164.             prop.locale = loc
  165.             try:
  166.                 locale.setlocale(locale.LC_ALL, locale.normalize(loc))
  167.             except locale.Error:
  168.                 pass
  169.  
  170.         toolbox = DevMgr4(r, __version__, device_uri, disable_dbus)
  171.         app.setMainWidget(toolbox)
  172.  
  173.         toolbox.show()
  174.  
  175.         try:
  176.             try:
  177.                 log.debug("Starting GUI loop...")
  178.                 app.exec_loop()
  179.             except KeyboardInterrupt:
  180.                 sys.exit(0)
  181.  
  182.         finally:
  183.             if child_pid:
  184.                 log.debug("Killing child toolbox process (pid=%d)..." % child_pid)
  185.                 try:
  186.                     os.kill(child_pid, signal.SIGKILL)
  187.                 except OSError, e:
  188.                     log.debug("Failed: %s" % e.message)
  189.  
  190.             mod.unlockInstance()
  191.             sys.exit(0)
  192.  
  193.     elif not disable_dbus: # qt3/dbus
  194.         # dBus
  195.         log.set_module("hp-toolbox(dbus)")
  196.         from base import device
  197.  
  198.         try:
  199.             # child (dbus connector)
  200.             os.close(r)
  201.  
  202.             dbus_loop = DBusGMainLoop(set_as_default=True)
  203.  
  204.             try:
  205.                 session_bus = dbus.SessionBus()
  206.             except dbus.exceptions.DBusException, e:
  207.                 if os.getuid() != 0:
  208.                     log.error("Unable to connect to dbus session bus. Exiting.")
  209.                     sys.exit(1)
  210.                 else:
  211.                     log.error("Unable to connect to dbus session bus (running as root?)")
  212.                     sys.exit(1)
  213.  
  214.             # Receive events from the session bus
  215.             session_bus.add_signal_receiver(handle_session_signal, sender_keyword='sender',
  216.                 destination_keyword='dest', interface_keyword='interface',
  217.                 member_keyword='member', path_keyword='path')
  218.  
  219.             log.debug("Entering main loop...")
  220.  
  221.             try:
  222.                 MainLoop().run()
  223.             except KeyboardInterrupt:
  224.                 log.debug("Ctrl-C: Exiting...")
  225.  
  226.         finally:
  227.             if parent_pid:
  228.                 log.debug("Killing parent toolbox process (pid=%d)..." % parent_pid)
  229.                 try:
  230.                     os.kill(parent_pid, signal.SIGKILL)
  231.                 except OSError, e:
  232.                     log.debug("Failed: %s" % e.message)
  233.  
  234.             mod.unlockInstance()
  235.  
  236.         sys.exit(0)
  237.  
  238. else: # qt4
  239.     try:
  240.         from PyQt4.QtGui import QApplication
  241.  
  242.     except ImportError:
  243.         log.error("Unable to load Qt4 support. Is it installed?")
  244.         sys.exit(1)
  245.  
  246.     from ui4.devmgr5 import DevMgr5
  247.  
  248.     log.set_module("hp-toolbox(UI)")
  249.  
  250.     if 1:
  251.     #try:
  252.         app = QApplication(sys.argv)
  253.  
  254.         toolbox = DevMgr5(__version__, device_uri,  None)
  255.         toolbox.show()
  256.         try:
  257.             log.debug("Starting GUI loop...")
  258.             app.exec_()
  259.         except KeyboardInterrupt:
  260.             sys.exit(0)
  261.  
  262.     if 1:
  263.     #finally:
  264.         mod.unlockInstance()
  265.         sys.exit(0)
  266.